import json

from flask import Flask, Blueprint, Response, request
from flask_jwt_extended import jwt_required
from app.common.commonMessage import CommonMassages
from app.service.wareHouseService import wareHouseService
from app.config.db import DBUtils
from app.config.jsonEncoder import CustomJSONEncoder

app = Flask(__name__)
# 创建蓝图
ware_house_up = Blueprint('ware_house_controller', __name__ , url_prefix='/kshx')

ware_house = wareHouseService()

# 获取session
db_utils = DBUtils()
session = db_utils.get_session()


# 获取客商画像-基础信息标签页-股东情况
@ware_house_up.route('/ware-house', methods=['GET'])
@jwt_required()
def warehouse():
    pagenum = request.args.get('pagenum')
    pagesize = request.args.get('pagesize')
    dataType = request.args.get('dataType')
    company = request.args.get('company')
    year = request.args.get('year')
    risktype = request.args.get('fxlx')

    if dataType == 'gsxx':
        # 工商信息
        data_info = ware_house.select_company_info(session, pagenum, pagesize, company)
    elif dataType == 'ggxx':
        # 股东信息
        data_info = ware_house.select_user_position(session, pagenum, pagesize, company)
    elif dataType == 'sjkzr':
        # 实际控制人
        data_info = ware_house.select_controller(session, pagenum, pagesize, company)
    elif dataType == 'risktitle':
        data_info = ware_house.select_risk_title(session, pagenum, pagesize, company, year, risktype)

    if data_info is not None:
        result = {
            "code": 200,
            "data": data_info,
            "message": CommonMassages.CCD_SELECT_SUCCESS.value
        }

    else:
        result = {
            "code": 400,
            "data": None,
            "message": CommonMassages.CCD_SELECT_FAILED.value
        }

    resp_data = json.dumps(result, ensure_ascii=False, cls=CustomJSONEncoder)
    # return Response(resp_data, content_type='application/json')
    return Response(resp_data)
